(function() {
    angular.module('app').component('globalCategoryComponent', {
        css: {
            preload: true,
            persist: true,
            href: 'app/components/global.category.component/global.category.component.css'
        },
        templateUrl: 'app/components/global.category.component/global.category.component.html',
        controller: globalCategoryComponent,
        controllerAs: 'vm',
        bindings: {
            listDefault: '<',
            categoryOption: '=',
            selectText: '<',
            isLeaf: '<',
            category: '=',
            type: '<' //all->全行业；singel->单行业 ;multi->多行业
        }
    });

    globalCategoryComponent.$inject = ['globalCategoryComponentSservice', '$rootScope', 'userService', '$timeout', 'util'];

    function globalCategoryComponent(globalCategoryComponentSservice, $rootScope, userService, $timeout, util) {
        var vm = this;
        // console.log('global-category-component js loading');
        // console.log($rootScope.userInfo);

        // console.log(vm.categoryOption);
        //vm.selectText
        //vm.type
        //

        //var Category = $resource();

        vm.hover = false;
        vm.tree = [];
        vm.treeController = {};


        vm.show = showElement;

        vm.hide = hideElement;

        console.log($rootScope.userInfo);

        switchType($rootScope.userInfo);



        function treeSelect(branch) {

            // branch.expanded =  !branch.expanded;

            // branch.expanded = !branch.expanded;


            if (branch.noLeaf) {
                vm.selectText = branch.label;
                hideElement();
                $rootScope.$emit('GLOBAL_CATEGORY_CHANGE_EV', branch.data.detailCatName, branch.data.category);

                vm.category = branch.data.category;
                return;
            }

            if (branch.children.length > 0 && !branch.noLeaf) {
                return;
            }

            // console.log(branch);

            var cat = branch.data.detailCatName;



            globalCategoryComponentSservice.getCat(cat).then(function(data) {

                var child = [];

                if (data.length == 0) {
                    branch.noLeaf = true;
                    return;
                }
                angular.forEach(data, function(menu2) {
                    child.push({
                        label: menu2.detailCatName,
                        noLeaf: menu2.category === null ? false : true,
                        children: [],
                        onSelect: treeSelect,
                        data: {
                            detailCatName: cat + '--' + menu2.detailCatName,
                            category: menu2.category
                        }
                    });
                });

                branch.children = child;


            }, function(err) {

            });

        }


        function switchType(user) {


            var type = util.returnUserIndustryType(user);



            if (vm.type == 'all') {
                _getAll();
                return;

            }


            switch (type) {
                case 'all': //全行业

                    _getAll();
                    break;
                case 'singel': //单行业

                    _getSingel();
                    break;
                case 'mutil': //多行业

                    _getMulti();
                    break;
                default:
                    break;
            }
        }


        function _getAll() {
            // console.log('AAAAAAAAAAAAAAAAAAAAA');
            // console.log(vm.selectText);
            // console.log(!vm.selectText);
            if (!vm.selectText) {
                $timeout(function() {
                    vm.selectText = "全类目";
                }, 0);

            }
            globalCategoryComponentSservice.get().then(function(data) {

                var tree = [];
                angular.forEach(data, function(item) {
                    tree.push({
                        label: item,
                        data: {
                            detailCatName: item
                        },
                        children: [],
                        onSelect: treeSelect
                    });
                });



                // console.log('vm.listDefault:' + vm.listDefault);
                if (vm.listDefault == false) {

                } else {
                    tree.unshift({
                        detailCatNameSplit: '',
                        detailCatName: '全类目',
                        hasSubMenus: false,
                        expanding: false,
                        submenus: []
                    });

                }


                vm.tree = tree;

            }, function(err) {

            });
        }

        function _getSingel() {
            if (!vm.selectText || vm.selectText.length == 0) {
                vm.selectText = "";
            }

            var itemLevel1Category = $rootScope.userInfo.itemLevel1Category;

            var detailCatName = itemLevel1Category.split('--')[1];

            $timeout(function() { vm.selectText = detailCatName; }, 0);

            // console.log(vm.selectText);
            // vm.menus = [{
            //     detailCatNameSplit: itemLevel1Category,
            //     detailCatName: detailCatName,
            //     hasSubMenus: true,
            //     expanding: false,
            //     submenus: []
            // }];

            globalCategoryComponentSservice.getCat(itemLevel1Category).then(function(data) {

                var tree = [];
                angular.forEach(data, function(item) {
                    tree.push({
                        label: item,
                        data: {
                            detailCatName: item
                        },
                        children: [],
                        onSelect: treeSelect
                    });
                });

                vm.tree = tree;

            }, function(err) {

            });


        }

        function _getMulti() {
            if (!vm.selectText || vm.selectText.length == 0) {
                $timeout(function() { vm.selectText = '类目'; }, 0);
            }

            var itemLevel1Category = $rootScope.userInfo.itemLevel1Category;

            var arry = itemLevel1Category.split(';');

            var detailCatName = arry[0].split('--')[1];

            // $timeout(function() { vm.selectText = detailCatName; }, 0);

            // console.log(vm.selectText);

            vm.menus = [];

            for (var i in arry) {
                vm.tree.push({
                    label: arry[i].split('--')[1],
                    data: {
                        detailCatName: arry[i]
                    },
                    children: [],
                    onSelect: treeSelect
                });

            }



        }

        function showElement() {
            $('.globalCategory').find('.allCats').addClass('hover');
        }

        function hideElement() {
            $('.globalCategory').find('.allCats').removeClass('hover');
        }
    }
})();
